<template>
  <div>
    <div>子组件{{num}}</div>
    <GrandSon />
    <button @click='handleSend'>点击</button>
  </div>
</template>
<script>
import GrandSon from './GrandSon.vue'
import { inject } from 'vue'

export default {
  name: 'Child',
  // 声明子组件触发的事件
  emits: ['send-money'],
  components: { GrandSon },
  props: {
    money: {
      type: Number,
      default: 0
    }
  },
  // setup的第一个形参，用于获取父传子的值
  setup (props, { emit }) {
    console.log(props.money)
    // 子组件把数据传递回父组件
    const handleSend = () => {
      // this.$emit('send-money', 50)
      emit('send-money', 50)
    }

    const num = inject('num')
    return { handleSend, num }
  }
}
</script>
